﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using CuaHangLaptop.BIZ;

namespace CuaHangLaptop.DAL
{
    class Kho_CTHD_DAL
    {
        internal static string Get_CTKho_ByCTHD(string _macthd)
        {
            string _ctkho = "";
            SqlConnection conn = Connection.Connection_String();
            SqlDataReader rdr = null;
            try
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand("select MaCTKho from Kho_CTHD where MaCTPhieuNhap='" + _macthd + "'", conn);
                rdr = cmd.ExecuteReader();
                rdr.Read();
                _ctkho = rdr.GetString(0);
            }
            catch
            {
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
            return _ctkho;
        }

        internal static void Insert(string _maCTHD, string _maCTKho)
        {
            SqlDataReader rdr = null;
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format(@"insert into Kho_CTHD values('{0}', '{1}')", _maCTHD, _maCTKho);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                if (rdr != null) rdr.Close();
                conn.Close();
            }
        }

        internal static void Update(string _maCTHD, string _maCTKho)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("update Kho_CTHD set MaCTKho='{0}' where MaCTHoaDon='{1}'", _maCTKho, _maCTHD);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }

        internal static void Delete(string _mactHD)
        {
            SqlConnection conn = Connection.Connection_String();
            try
            {
                conn.Open();
                String s = String.Format("delete from Kho_CTHD where MaCTHoaDon='{0}'", _mactHD);
                SqlCommand cmd = new SqlCommand(s, conn);

                if (cmd.ExecuteNonQuery() > 0)
                {
                }
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
